/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.server.DAO;

import db.server.DTO.DesignTableDTO;
import db.server.DTO.RowDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author QuangKhai
 */
public class DesignDAO {
    private Document document = null;
    
    public DesignDAO(){
        DocumentBuilder builder = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DesignDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        document = builder.newDocument();
    }
    
    public void writeDesignTable(DesignTableDTO table, String path){
        // Insert Root Table        
        Element root = (Element) document.createElement("Table");
        root.setAttribute("name", table.getTableName());
        document.appendChild(root);
        
        for(int i = 0; i < table.getLstRows().size(); i++){
            Element column = (Element) document.createElement("Column");
            column.setAttribute("name", table.getLstRows().get(i).getColumnName());
            column.setAttribute("type", table.getLstRows().get(i).getDataType());
            column.setAttribute("null", table.getLstRows().get(i).isAllowNulls() + "");
            column.setAttribute("primary", table.getLstRows().get(i).isPrimaryKey() + "");

            root.appendChild(column);
        }
        
        // Normalizing the DOM
        document.getDocumentElement().normalize();
        
        String fileName = "";
        for(int i = 0; i < path.length(); i++){
            if(path.charAt(i) == '.'){
                fileName = path;                
                DOMUtil.writeXmlToFile(fileName, document);
                return;
            }
        }
        
        fileName = path + "\\" + table.getTableName() + "-Design" + ".xml";    
        DOMUtil.writeXmlToFile(fileName, document);        
    }
    
    public DesignTableDTO readDesignTable(String designPath){
        DesignTableDTO table = new DesignTableDTO();
        table.setTablePath(designPath);
        
        document = DOMUtil.parse(designPath);
        
        Element root = document.getDocumentElement();
        table.setTableName(root.getAttribute("name"));
        
        NodeList childNode = root.getChildNodes();
        
        int num = childNode.getLength();
        
        List<RowDTO> lstRow = new ArrayList<RowDTO>();
        
        for(int i = 0; i < num; i++){
            Element node = (Element) childNode.item(i);
            
            RowDTO row = new RowDTO();
            
            row.setColumnName(node.getAttribute("name"));
            row.setDataType(node.getAttribute("type"));
            
            if(node.getAttribute("null").compareTo("true") == 0){
                row.setAllowNulls(true);
            } else {
                row.setAllowNulls(false);
            }
            
            if(node.getAttribute("primary").compareTo("true") == 0){
                row.setPrimaryKey(true);
            } else {
                row.setPrimaryKey(false);
            }
            
            lstRow.add(row);
        }                
        
        table.setLstRows(lstRow);
        
        return table;
    }
}
